/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.insurance.service.impl;

import me.zhengjie.modules.insurance.domain.InsuredApply;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.insurance.repository.InsuredApplyRepository;
import me.zhengjie.modules.insurance.service.InsuredApplyService;
import me.zhengjie.modules.insurance.service.dto.InsuredApplyDto;
import me.zhengjie.modules.insurance.service.dto.InsuredApplyQueryCriteria;
import me.zhengjie.modules.insurance.service.mapstruct.InsuredApplyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author HeHui
* @date 2022-01-03
**/
@Service
@RequiredArgsConstructor
public class InsuredApplyServiceImpl implements InsuredApplyService {

    private final InsuredApplyRepository insuredApplyRepository;
    private final InsuredApplyMapper insuredApplyMapper;

    @Override
    public Map<String,Object> queryAll(InsuredApplyQueryCriteria criteria, Pageable pageable){
        Page<InsuredApply> page = insuredApplyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(insuredApplyMapper::toDto));
    }

    @Override
    public List<InsuredApplyDto> queryAll(InsuredApplyQueryCriteria criteria){
        return insuredApplyMapper.toDto(insuredApplyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public InsuredApplyDto findById(Long id) {
        InsuredApply insuredApply = insuredApplyRepository.findById(id).orElseGet(InsuredApply::new);
        ValidationUtil.isNull(insuredApply.getId(),"InsuredApply","id",id);
        return insuredApplyMapper.toDto(insuredApply);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public InsuredApplyDto create(InsuredApply resources) {
        return insuredApplyMapper.toDto(insuredApplyRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(InsuredApply resources) {
        InsuredApply insuredApply = insuredApplyRepository.findById(resources.getId()).orElseGet(InsuredApply::new);
        ValidationUtil.isNull( insuredApply.getId(),"InsuredApply","id",resources.getId());
        insuredApply.copy(resources);
        insuredApplyRepository.save(insuredApply);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            insuredApplyRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<InsuredApplyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (InsuredApplyDto insuredApply : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建人 用户申请为0 后台创建为操作人ID", insuredApply.getCreateUserId());
            map.put("是否删除", insuredApply.getIsDeleted());
            map.put("创建日期", insuredApply.getCreateTime());
            map.put("最后更改时间", insuredApply.getLastUpdateTime());
            map.put("最后修改人 后台用户ID", insuredApply.getLastUpdateId());
            map.put("案件申请人ID", insuredApply.getInsuredUserId());
            map.put("案件所属公司ID", insuredApply.getCompanyId());
            map.put("案件所属公司站点ID", insuredApply.getCompanySiteId());
            map.put("站长姓名", insuredApply.getSiteUserName());
            map.put("站长手机", insuredApply.getSiteUserMobile());
            map.put("报案号", insuredApply.getReportNo());
            map.put("保单号", insuredApply.getPolicyNo());
            map.put("骑手名称", insuredApply.getUserName());
            map.put("出险人手机号码", insuredApply.getUserMobile());
            map.put("骑手身份证", insuredApply.getUserIdCard());
            map.put("理赔对象，1骑手,2公司,3三者", insuredApply.getClaimType());
            map.put("责任判断ids", insuredApply.getResponsibilityIds());
            map.put("有无附加险", insuredApply.getAdditional());
            map.put("案件进度", insuredApply.getCaseSpeek());
            map.put("事故发生时间", insuredApply.getAccidentDate());
            map.put("出险类型", insuredApply.getAccidentTypeIds());
            map.put("事故区域code", insuredApply.getAccidentRegionCode());
            map.put("事故地址", insuredApply.getAccidentAddress());
            map.put("事故说明", insuredApply.getAccidentDesc());
            map.put("理赔员ID，后台用户ID", insuredApply.getResponsibleUserId());
            map.put("案件状态", insuredApply.getCaseStatus());
            map.put("主险赔付金额", insuredApply.getMainInsuredCompensationAmount());
            map.put("附加险赔付金额", insuredApply.getAdditionalMainInsuredCompensationAmount());
            map.put("其他金额", insuredApply.getOtherAmount());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}